package com.elaine.factory.alapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.elaine.constant.AlapiConstant;
import com.elaine.model.alapi.AlapiResponse;
import com.elaine.model.alapi.CommentData;
import com.elaine.utils.HttpUtils;
import org.springframework.stereotype.Component;

/**
 * @author wrb
 * @date 2020/3/31 17:35
 */
@Component
public class CommentAlapiFactory extends AlapiFactory {

	private static final String URL = "https://v1.alapi.cn/api/comment?format=json";

	@Override
	protected void registerFactory() {
		doRegister(AlapiConstant.COMMENT, this);
	}

	@Override
	public String getReply() {
		String jsonStr = HttpUtils.requestGet(URL, null, null);
		AlapiResponse<CommentData> alapiResponse = JSON.parseObject(jsonStr, new TypeReference<AlapiResponse<CommentData>>() {
		});
		if (alapiResponse != null && alapiResponse.isSuccess()) {
			CommentData data = alapiResponse.getData();
			return "歌曲：" + data.getTitle() + "\n歌手：" + data.getAuthor() + "\n专辑：" + data.getAlbum() + "\n\n" + data.getComment_nickname() + "：" + data.getComment_content();
		} else {
			return "网易云乐评请求失败咯，wrwrwr...";
		}
	}

}
